using System;
using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine;
using UnityEngine.UI;



namespace BackpackSystem
{
    /// <summary>
    /// 
    /// </summary>
    public class InventoryUI : MonoBehaviour
    {

        [Header("库存栏")]
        [Tooltip("库存栏|节点"), SerializeField] Transform m_InventoryRoot;
        [Tooltip("库位项|制件"), SerializeField] GameObject m_InventorySlotPrefab;
        [Tooltip("物品项|制件"), SerializeField] GameObject m_InventoryItemPrefab;




        #region Property
        [Description("依赖画布")] public Canvas CanvasRoot;
        [Description("依赖RECT")] public RectTransform InventoryRootRT;
        #endregion

        #region Event

        public event EventHandler<ItemAppendEvent> ItemAppendEvent;
        public event EventHandler<ItemRemoveEvent> ItemRemoveEvent;
        public event EventHandler<ItemSelectEvent> ItemSelectEvent;
        #endregion

        #region Cache

        /// <summary>
        /// 
        /// </summary>
        public Dictionary<Vector2, ItemSlot> ItemSlotLib { get; private set; }

        #endregion





        /// <summary>
        /// 
        /// </summary>
        public void InitEnvironment()
        {
            this.CanvasRoot = this.transform.parent.GetComponent<Canvas>();
            this.InventoryRootRT = this.m_InventoryRoot?.GetComponent<RectTransform>();

            this.ItemSlotLib = new Dictionary<Vector2, ItemSlot>();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="rows"></param>
        /// <param name="colums"></param>
        public void InitInventory(uint rows, uint colums)
        {
            uint total = rows * colums;

            ItemSlotLib = new Dictionary<Vector2, ItemSlot>();

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < colums; j++)
                {
                    Vector2 pointTags = new Vector2(i, j);
                    ItemSlot itemSlot = GameObject.Instantiate(m_InventorySlotPrefab, m_InventoryRoot).GetComponent<ItemSlot>();

                    this.ItemSlotLib.TryAdd(pointTags, itemSlot);

                    itemSlot.gameObject.name = $"背包编号 [{i}-{j}]";
                }
            }


            LayoutRebuilder.ForceRebuildLayoutImmediate(m_InventoryRoot.GetComponent<RectTransform>());
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="itemType"></param>
        public void Add(ItemType itemType)
        {
            // P1：检查 物品槽位是否已满 
            bool isGetSlot = this.TryGetEmptyItemSlot(out ItemSlot itemSlot);
            if (!isGetSlot) return;

            // P2：检查 资产库是否存在资产
            bool isGetItem = InventoryAsset.TryGetItem(itemType, out ItemBase itemBase);
            if (!isGetItem) return;

            // P3：实例化 UI
            Item item = GameObject.Instantiate(this.m_InventoryItemPrefab, transform).GetComponent<Item>();

            this.ItemAppendEvent?.Invoke(this, new ItemAppendEvent(item, itemSlot));

            itemSlot.DoUpdateDetail(item);

            item.DoBind(this, itemBase, itemSlot);
            item.DoUpdate();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="itemBase"></param>
        public void Submit(ItemBase itemBase)
        {
            this.ItemSelectEvent?.Invoke(this, new BackpackSystem.ItemSelectEvent(itemBase));
        }
    }
}
